import { handlerRegister } from '../fn/register'; // 导入需要测试的函数
import ls from '../../src/renderer/utils/crypto';

jest.mock('../../src/renderer/utils/crypto', () => ({
  getItem: jest.fn()
}));

describe('handlerRegister', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('should return res if limit is greater than current time', () => {
    const mockCode = 'mockCode';
    const mockItem = JSON.stringify({ limit: new Date(2099, 0, 1) });

    ls.getItem.mockReturnValue(mockItem); // 模拟 ls.getItem 返回值

    const result = handlerRegister(mockCode);

    expect(ls.getItem).toHaveBeenCalledWith(mockCode);
    expect(result).toEqual(JSON.parse(mockItem));
  });

  it('should return undefined if limit is less than current time', () => {
    const mockCode = 'mockCode';
    const mockItem = JSON.stringify({ limit: new Date(2000, 0, 1) }); // 模拟存储在 ls 中的过期数据

    ls.getItem.mockReturnValue(mockItem); // 模拟 ls.getItem 返回值

    const result = handlerRegister(mockCode);

    expect(ls.getItem).toHaveBeenCalledWith(mockCode);
    expect(result).toBeUndefined();
  });

  it('should return undefined if code is undefined', () => {
    const mockCode = 'mockCode';
    const mockItem = ''; // 模拟存储在 ls 中的过期数据

    ls.getItem.mockReturnValue(mockItem); // 模拟 ls.getItem 返回值

    const result = handlerRegister(mockCode);

    expect(ls.getItem).toHaveBeenCalledWith(mockCode);
    expect(result).toBeUndefined();
  });
});